﻿
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Apps.Models;
using Apps.Common;
using Apps.BLL.Core;
using System.Transactions;
using Apps.Models.MIS;
using Apps.IBLL.MIS;
using Apps.IDAL.MIS;
using Apps.IDAL;
using Apps.Locale;
using Apps.IDAL.Sys;

namespace Apps.BLL.MIS
{
    public partial class MIS_ArticleBLL
    {

        [Dependency]
        public IMIS_Article_CategoryRepository categoryRep { get; set; }
        [Dependency]
        public ISysUserRepository userRep { get; set; }

        public override MIS_ArticleModel GetById(object id)
        {
            if (IsExists(id))
            {
                MIS_Article entity = m_Rep.GetById(id);
                MIS_ArticleModel model = new MIS_ArticleModel();
                model.Id = entity.Id;
                model.ChannelId = entity.ChannelId;
                model.CategoryId = entity.CategoryId;
                model.Title = entity.Title;
                model.CreaterName = entity.SysUser.TrueName;
                model.ImgUrl = entity.ImgUrl;
                model.BodyContent = entity.BodyContent;
                model.Sort = entity.Sort;
                model.Click = entity.Click;
                model.CheckFlag = entity.CheckFlag;
                model.Checker = entity.Checker;
                model.CheckDateTime = entity.CheckDateTime;
                model.Creater = entity.Creater;
                model.CreateTime = entity.CreateTime;
                model.CustomLink = entity.CustomLink;
                model.IsType = entity.IsType;
                model.CategoryIdParent = entity.MIS_Article_Category.ParentId;
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pager">JQgrid分页</param>
        /// <param name="queryStr">搜索条件</param>
        /// <returns>列表</returns>
        public List<MIS_ArticleModel> GetList(ref GridPager pager, string queryStr,string cid,bool isManage,string userId,int check)
        {
            IQueryable<MIS_Article> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(a => a.Title.Contains(queryStr) || a.BodyContent.Contains(queryStr));
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            if (!string.IsNullOrWhiteSpace(cid))
            {
                queryData = m_Rep.GetList(a => a.CategoryId==cid);
            }
       
            //审批状态
            switch (check)
            {
                case 1://全部
                    break;
                case 2://已审批
                    queryData = queryData.Where(a => a.CheckFlag == 1);
                    break;
                case 3://未审批
                    queryData = queryData.Where(a => a.CheckFlag == 0);
                    break;
                default:
                    break;
            }
 	    pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);

           
           
            return CreateModelList(ref queryData);
        }


        public override List<MIS_ArticleModel> CreateModelList(ref IQueryable<MIS_Article> queryData)
        {

            List<MIS_ArticleModel> modelList = (from r in queryData
                                                select new MIS_ArticleModel
                                                {
                                                    Id = r.Id,
                                                    ChannelId = r.ChannelId,
                                                    CategoryId = r.CategoryId,
                                                    CategoryName = r.MIS_Article_Category.Name,
                                                    Title = r.Title,
                                                    ImgUrl = r.ImgUrl,
                                                    BodyContent = r.BodyContent,
                                                    Sort = r.Sort,
                                                    Click = r.Click,
                                                    CheckFlag = r.CheckFlag,
                                                    Checker = r.Checker,
                                                    CheckerName = r.SysUser1.TrueName,
                                                    CheckDateTime = r.CheckDateTime,
                                                    Creater = r.Creater,
                                                    CreaterName = r.SysUser.TrueName,
                                                    CreateTime = r.CreateTime,
                                                    CustomLink = r.CustomLink,
                                                    IsType = r.IsType,

                                                }).ToList();

            return modelList;
        }

        public List<MIS_ArticleModel> GetListByIsType(ref GridPager pager, int isType)
        {
            IQueryable<MIS_Article> queryData = null;
           
                queryData = m_Rep.GetList().Where(a=>a.IsType==isType).OrderBy(a=>a.CreateTime);
            pager.totalRows = queryData.Count();
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
       
        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="errors">持久的错误信息</param>
        /// <param name="id">id</param>
        /// <returns>是否成功</returns>
        public bool Delete(ref ValidationErrors errors, string id,string userId)
        {
            try
            {
                if (m_Rep.Delete(id, userId) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }
      
        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="errors">持久的错误信息</param>
        /// <param name="id">主键</param>
        /// <param name="checkFlag">1审批0反审批</param>
        /// <param name="checker">审批人</param>
        /// <returns>是否成功</returns>
        public bool Check(ref ValidationErrors errors, string id, int checkFlag, string checker)
        {
            try
            {
                MIS_Article entity = m_Rep.GetById(id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.CheckFlag = checkFlag;
                entity.Checker = checker;
                entity.CheckDateTime = DateTime.Now;

                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.CheckFail);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

     
    }
}
